/* ************************************
 * GazeVis
 * 
 * ************************************
 */
 
static final color FIXATION_COLOR = #FF4646;


// 
// class GazeVis
// ________________________________________________________________________________________________________
class GazeVis
{
	//Attributes
	GazeLog gazeData;
	MoveTracker moves;
	int curT;
	int lastT;
	int nextT;
	int curIndex;
	int state;
	
	//Constuctor
	GazeVis( GazeLog gazeData )
	{
		// initialize data
		this.moves = new MoveTracker();
		this.gazeData = gazeData;
		curT = 0;
		nextT = 0;
		curIndex = 0;
		nextT = -1;
		lastT = gazeData.getLastTimestamp();
		
		// state
		state = STATE_PAUSED;
	}
		
	boolean update(int deltaMillis)
	{
		boolean stopPlay = false;
		int newT = nextT + round( (float) deltaMillis * timeScale );
		if (newT == curT)
		{
			return false;
		}
		
		if (newT < 0)
		{
			newT = 0;
		} 
		else if (newT > lastT)
		{
			newT = lastT;
			stopPlay = true;
		}
		
		int newIndex;		
		if (newT < curT)
		{
			newIndex = gazeData.seek( newT );
		}
		else
		{
			newIndex = gazeData.quickSeek( curIndex, newT );
		}
		
		curT = newT;
		nextT = newT;
		curIndex = newIndex;
		
		return stopPlay;
	}
	
	void draw()
	{
		Fixation fix = gazeData.getFixation( curIndex );
                
		if (draw_lines)
		{
			moves.update( fix );
			moves.draw();
		}
		
        // draw a cross
        /*
         pushStyle();
         {
            stroke( FIXATION_COLOR );
            strokeWeight(2.5);
			
            float x = fix.x;
            float y = fix.y;
            
            line( x - 15 , y, x + 15 , y );
            line( x  , y-15, x , y+15 );
         }	
         popStyle();
         */

	}
	
	//void updateMoves( ) 
	//	
	void changeState( int state ) 
	{
		moves.changeState( state );
	}

    void clearMoves( ) 
	{
		moves.reset( );
	}

    
	void updateT( int newT )
	{
		nextT = newT;
		if (nextT < 0) {
			nextT = 0;
		}
		else if (nextT > lastT)
		{
			nextT = lastT;
		}
		update(0);
	}
	
	boolean render(int deltaMillis)
	{
		boolean retVal = false;
		if ( state == STATE_PLAYING )
		{
			if ( update(deltaMillis) )
			{
				state = STATE_PAUSED;
				retVal = true;
			}
		}
		
		// draw
		draw();
		
		return retVal;
	}
	
		
	void jumpToNextFixation()
	{
		curIndex++;
		if (curIndex >= gazeData.getFixationCount())
		{
			curIndex = gazeData.getFixationCount() - 1;
		}
		curT = gazeData.getFixation( curIndex ).timestamp;
	}
	
	void jumpToPreviousFixation()
	{
		if (curIndex > 0)
		{
			curIndex--;
			curT = gazeData.getFixation( curIndex ).timestamp;
		}
	}
	
	int getCurrentT()
	{
		return curT;
	}
	
	void setPlayState( int s )
	{
		state = s;
	}
}

